package cn.iocoder.yudao.module.cont.service.base;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.cont.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PlanInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cont.dal.mysql.base.PlanInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cont.enums.ErrorCodeConstants.*;

/**
 * 合同收付款计划表  Service 实现类
 *
 * @author 测试用户
 */
@Service
@Validated
public class PlanInfoServiceImpl implements PlanInfoService {

    @Resource
    private PlanInfoMapper planInfoMapper;

    @Override
    public Long createPlanInfo(PlanInfoSaveReqVO createReqVO) {
        // 插入
        PlanInfoDO planInfo = BeanUtils.toBean(createReqVO, PlanInfoDO.class);
        planInfoMapper.insert(planInfo);
        // 返回
        return planInfo.getId();
    }

    @Override
    public void updatePlanInfo(PlanInfoSaveReqVO updateReqVO) {
        // 校验存在
        validatePlanInfoExists(updateReqVO.getId());
        // 更新
        PlanInfoDO updateObj = BeanUtils.toBean(updateReqVO, PlanInfoDO.class);
        planInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePlanInfo(Long id) {
        // 校验存在
        validatePlanInfoExists(id);
        // 删除
        planInfoMapper.deleteById(id);
    }

    private void validatePlanInfoExists(Long id) {
        if (planInfoMapper.selectById(id) == null) {
            throw exception(PLAN_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PlanInfoDO getPlanInfo(Long id) {
        return planInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PlanInfoDO> getPlanInfoPage(PlanInfoPageReqVO pageReqVO) {
        return planInfoMapper.selectPageDue(pageReqVO);
    }

}